//
//  RetrievePasswordViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/6/7.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "RetrievePasswordViewController.h"

@interface RetrievePasswordViewController ()
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UIScrollView *scroview;
@property (strong, nonatomic) IBOutlet UIView *twoBackgroundView;
@property (strong, nonatomic) IBOutlet UITextField *phoneNumber;//请输入手机号
@property (weak, nonatomic) IBOutlet UIButton *getNumberButton;
@property (strong, nonatomic) IBOutlet UITextField *VerificationNuber;//请输入验证码
@property (strong, nonatomic) IBOutlet UITextField *payPassword;//请输入支付密码
@property (strong, nonatomic) IBOutlet UITextField *confirmPayPassword;//确认支付密码

@property(nonatomic,strong)NSTimer * timer;
@property(nonatomic,assign)NSInteger  time;
@end

@implementation RetrievePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroview];
    [self adaptiveViewLayout:self.twoBackgroundView];
    
    if (_titleStr) {
        _titleLabel.text = _titleStr;
    }
    
    _scroview.contentSize = flexibleSize(CGSizeMake(0, 505), NO);
}
#pragma mark - 返回
- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}
#pragma mark - 请求验证码
- (IBAction)GetVerificationNumber:(id)sender {
    if ([Common dataIsNULL:_phoneNumber.text]) {
        [CWHUDTips showTips:@"请输入手机号码"];
        return;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_phoneNumber.text forKey:@"phone"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_getPayPwdCode withHttpType:@"POST" withTag:SX_SX_getPayPwdCode];
}
#pragma mark - 确认
- (IBAction)confirmButton:(id)sender {
    if ([Common dataIsNULL:_phoneNumber.text]) {
        [CWHUDTips showTips:@"请输入手机号码"];
        return;
    }
    
    if ([Common dataIsNULL:_VerificationNuber.text]) {
        [CWHUDTips showTips:@"请输入验证码"];
        return;
    }
    if ([Common dataIsNULL:_payPassword.text]) {
        [CWHUDTips showTips:@"请输入密码"];
        return;
    }
    if ([Common dataIsNULL:_confirmPayPassword.text]) {
        [CWHUDTips showTips:@"请再次输入密码"];
        return;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_phoneNumber.text forKey:@"userName"];
    [dic setObject:_VerificationNuber.text forKey:@"code"];
    [dic setObject:[Common shaEncrypt:_payPassword.text] forKey:@"password"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_updatePayPwd withHttpType:@"POST" withTag:SX_SX_updatePayPwd];
}


#pragma mark -- Timer

- (void)changeTime{
    
    _time ++;
    NSString * time = [NSString stringWithFormat:@"%ld秒后重发",60-_time];
    [_getNumberButton setTitle:time forState:UIControlStateNormal];
    [_getNumberButton setTitleColor:[UIColor colorWithWhite:0.773 alpha:1.000] forState:UIControlStateNormal];
    _getNumberButton.enabled = NO;
    if (_time == 60) {
        [self stopTimer];
        [_getNumberButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_getNumberButton setTitleColor:[UIColor colorWithRed:0.906 green:0.780 blue:0.086 alpha:1.000] forState:UIControlStateNormal];
        _getNumberButton.enabled = YES;
    }
}

- (void)startTimer{
    _time = 0;
    [_timer fire];
}

- (void)stopTimer{
    [_timer invalidate];
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == SX_SX_getPayPwdCode){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
        }else  {
             [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            _timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
            [self startTimer];
        }
    }
    
    if (tag == SX_SX_updatePayPwd){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
        }else  {
            [self.navigationController  popViewControllerAnimated:YES];
                  [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:2];
           
           
        }
    }

}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    [self dismissWaitDialog];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
